package zero.tech.games.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import zero.tech.redis.cache.RedisUtil;
@Component
public class FrozenUtil {
	@Autowired
	private RedisUtil redisUtil;

	public void insert(int club_id, int uid) {
		String str=(String) redisUtil.get(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id);
		if(StringUtils.isBlank(str)) {
			List<Integer> list=new ArrayList<>();
			list.add(uid);
			redisUtil.set(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id, JSONObject.toJSONString(list));
		}else {
			List<Integer> list=JSONObject.parseObject(str, new TypeReference<List<Integer>>() {});
			if(!list.contains(uid)) {
				list.add(uid);
				redisUtil.set(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id, JSONObject.toJSONString(list));
			}
		}
	}

	public boolean infrozenlist(Integer club_id, int uid) {
		String str=(String) redisUtil.get(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id);
		if(StringUtils.isNotBlank(str)) {
			List<Integer> list=JSONObject.parseObject(str, new TypeReference<List<Integer>>() {});
			if(list.contains(uid)) {
				return true;
			}
		}
		return false;
	}

	public void insertBatch(int club_id, List<Integer> uids) {
		String str=(String) redisUtil.get(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id);
		if(StringUtils.isBlank(str)) {
			List<Integer> list=new ArrayList<>();
			for(Integer uid:uids) {
				if(!list.contains(uid)) {
					list.add(uid);
				}
			}
			redisUtil.set(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id, JSONObject.toJSONString(list));
		}else {
			List<Integer> list=JSONObject.parseObject(str, new TypeReference<List<Integer>>() {});
			for(Integer uid:uids) {
				if(!list.contains(uid)) {
					list.add(uid);
				}
			}
			redisUtil.set(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id, JSONObject.toJSONString(list));
		}		
	}

	public List<Integer> getFrozenList(int club_id) {
		List<Integer> list=new ArrayList<>();
		String str=(String) redisUtil.get(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id);
		if(StringUtils.isNotBlank(str)) {
			List<Integer> tmp=JSONObject.parseObject(str, new TypeReference<List<Integer>>() {});
			list.addAll(tmp);
		}
		return list;
	}

	public void delete(int club_id, Integer uid) {
		String str=(String) redisUtil.get(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id);
		if(StringUtils.isNotBlank(str)) {
			List<Integer> list=JSONObject.parseObject(str, new TypeReference<ArrayList<Integer>>() {});
			if(list.contains(uid)) {
				list.remove(uid);
				redisUtil.set(RedisConsts.B_CLUBMEMBER_FROZEN+"_"+club_id, JSONObject.toJSONString(list));
			}
		}
	}
}
